package com.alibaba.citrus.ots.unified.inventory.facade.service;

import com.alibaba.citrus.ots.common.exception.FacadeException;
import com.alibaba.citrus.ots.common.util.QuantityFormatUtils;
import com.alibaba.citrus.ots.common.util.SapAndCrmFieldDealUtils;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.model.dto.*;
import com.epoch.app.bcots.service.BaseDataService;
import com.epoch.app.bcots.service.BcOtsService;
import com.epoch.app.bcots.service.UnifiedInventoryService;
import com.epoch.app.otsunifiedinventory.batchinventorycheckertemp.dto.LoadWarehouseResponse;
import com.epoch.app.otsunifiedinventory.batchinventorycheckertemp.model.BatchInventoryCheckerTemp;
import com.epoch.app.otsunifiedinventory.batchinventorycheckertemp.service.BatchInventoryCheckerTempService;
import com.epoch.app.otsunifiedinventory.domain.allocateorder.dto.AllocateOrderLoadListRequest;
import com.epoch.app.otsunifiedinventory.domain.allocateorder.model.AllocateOrder;
import com.epoch.app.otsunifiedinventory.domain.allocateorder.service.AllocateOrderService;
import com.epoch.app.otsunifiedinventory.domain.allocateorderdetail.dto.AllocateOrderDetailLoadListRequest;
import com.epoch.app.otsunifiedinventory.domain.allocateorderdetail.model.AllocateOrderDetail;
import com.epoch.app.otsunifiedinventory.domain.allocateorderdetail.service.AllocateOrderDetailService;
import com.epoch.app.otsunifiedinventory.inventorychecker.dto.*;
import com.epoch.app.otsunifiedinventory.inventorychecker.model.InventoryChecker;
import com.epoch.app.otsunifiedinventory.inventorychecker.service.InventoryCheckerService;
import com.epoch.app.otsunifiedinventory.model.dto.*;
import com.epoch.app.otsunifiedinventory.sdo.enums.InventoryCheckStatus;
import com.epoch.app.otsunifiedinventory.service.OtsUnifiedInventoryService;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Chen.mu
 * @date 2022/1/17
 */
@Service
@Primary
public class InventoryCheckerServiceImpl implements InventoryCheckerService {
    private static Log log = Log.getLogger(InventoryCheckerServiceImpl.class);
    @Autowired
    private OtsUnifiedInventoryService otsUnifiedInventoryService;
    @Resource
    private BaseDataService baseDataService;
    @Autowired
    private UnifiedInventoryService unifiedInventoryService;
    @Autowired
    private AllocateOrderService allocateOrderService;
    @Autowired
    private AllocateOrderDetailService allocateOrderDetailService;
    @Autowired
    private BatchInventoryCheckerTempService batchInventoryCheckerTempService;
    @Autowired
    private BcOtsService bcOtsService;

    /**
     * 批次维度 的 校验总库存
     *
     * @param request
     * @return
     */
    @Override
    public Result<List<InventoryCheckerRequest>> importInventoryData2Check(ImportInventoryData2CheckRequest request) {
//        Set<String> goodsId = new HashSet<>();
//        Set<String> warehouseCodes = new HashSet<>();
//        List<InventoryCheckerRequest> reslutList = request.getInventoryData();
//        //bivc:business inventory check
//        if (CollectionUtils.isNotEmpty(reslutList)) {
//            reslutList.stream().forEach(x -> {
//                        x.setGoodsCode(SapAndCrmFieldDealUtils.subStringScItmCode(x.getGoodsCode()));
//                        if (StringUtils.isEmpty(x.getGoodsCode())
//                                && StringUtils.isEmpty(x.getWarehouseCode())
//                                && StringUtils.isEmpty(x.getFatoryCode())) {
//                            //3个code都是空的 可能是最后一行 汇总行。 跳过
//                            return;
//                        }
//                        if (StringUtils.isEmpty(x.getGoodsCode())
//                                || StringUtils.isEmpty(x.getWarehouseCode())
//                                || StringUtils.isEmpty(x.getFatoryCode())) {
//                            x.setErrorMessage("excel数据有误，请检查工厂及仓库code 和物料编码不能为空");
//                        } else {
//                            goodsId.add(x.getGoodsCode());
//                            warehouseCodes.add(x.getWarehouseCode());
//                        }
//                    }
//            );
//        }
//        Map<String, ScItemSDO> goods = getGoodsListWithOuterId(goodsId);
//        Map<String, WarehouseInfo> warehouses = getWarehouseList(warehouseCodes, true);
//        List<InventoryCheckerRequest> inventoryCheckerList = new ArrayList<>();
//        reslutList.stream().forEach(x -> {
//            if (StringUtils.isEmpty(x.getGoodsCode())
//                    && StringUtils.isEmpty(x.getWarehouseCode())
//                    && StringUtils.isEmpty(x.getFatoryCode())) {
//                //3个code都是空的 可能是最后一行 汇总行。 跳过
//                return;
//            }
//            InventoryChecker inventoryChecker = new InventoryChecker();
//            WarehouseInfo warehouseInfo = warehouses.get(x.getFatoryCode() + "_" + x.getWarehouseCode());
//            inventoryChecker.setWarehouseCode(x.getWarehouseCode());
//            inventoryChecker.setFactoryCode(x.getFatoryCode());
//            inventoryChecker.setGoodsCode(x.getGoodsCode());
//            inventoryChecker.setExcelName(x.getExcelName());
//            InventoryCheckStatus failStatus = InventoryCheckStatus.OK;
//            BatchInventoryCheckStatus batchInventoryCheckStatus = BatchInventoryCheckStatus.OK;
//            //货品code 仓库code不能为空
//            if (warehouseInfo == null) {
//                failStatus = InventoryCheckStatus.WAREHOUSE_FAIL;
//            }
//            if (goods.get(x.getGoodsCode()) == null) {
//                failStatus = InventoryCheckStatus.GOODS_FAIL;
//            }
//            // BigDecimal availabelQuantity = new BigDecimal(0);
//            BigDecimal totalQuantity = new BigDecimal(0);
//            BigDecimal frozenQuantity = new BigDecimal(0);
//            BigDecimal inspectQuantity = new BigDecimal(0);
//            try {
//                totalQuantity = QuantityFormatUtils.formatQuantityForDB(x.getTotalQuantity());
//                frozenQuantity = QuantityFormatUtils.formatQuantityForDB(x.getFrozenQuantity());
//                inspectQuantity = QuantityFormatUtils.formatQuantityForDB(x.getInspectQuantity());
//            } catch (Exception e) {
//                failStatus = InventoryCheckStatus.QUANTITY_FORMAT_FAIL;
//            }
//            long totalGap = 0;
//            InventoryQueryResultForCheck inventoryCheck = null;
//            if (failStatus.getValue() > 0) {
//                inventoryCheck = getBizInventory(goods.get(x.getGoodsCode()).getScItemId(), warehouseInfo.getId(), x.getBatchNo(), 2);
//                if (inventoryCheck == null) {
//                    failStatus = InventoryCheckStatus.INVENTORY_FAIL;
//                }
//                if (inventoryCheck != null) {
////                    totalGap = totalQuantity.longValue() - inventoryCheck.getTotalQuantity();
//                    //sap总库存 = 非限制 + 已冻结 + 质检中
//                    long sapTotalQuantity = totalQuantity.longValue() + frozenQuantity.longValue() + inspectQuantity.longValue();
//                    totalGap = sapTotalQuantity - inventoryCheck.getTotalQuantity();
//                }
//                if (totalGap != 0) {
//                    failStatus = InventoryCheckStatus.INV_QUANTITY_FAIL;
//                }
//                if (failStatus.getValue() < 0) {
//                    inventoryCheckerList.add(x);
//                }
//            }
//            inventoryChecker.setBatchNo(x.getBatchNo());
//            inventoryChecker.setOutTotalQuantity(QuantityFormatUtils.formatQuantityForDB(x.getTotalQuantity()).longValue());
//            inventoryChecker.setTotalQuantityGap(totalGap);
//            if (inventoryCheck != null) {
//                inventoryChecker.setInventoryId(inventoryCheck.getId());
//                inventoryChecker.setInTotalQuantity(inventoryCheck.getTotalQuantity());
//            }
//            if (warehouseInfo != null) {
//                inventoryChecker.setWarehouseAreaId(warehouseInfo.getId());
//                inventoryChecker.setWarehouseId(warehouseInfo.getWarehouseId());
//            } else {
//                inventoryChecker.setWarehouseAreaId("-1");
//                inventoryChecker.setWarehouseId("-1");
//            }
//            if (goods.get(x.getGoodsCode()) != null) {
//                inventoryChecker.setGoodsId(goods.get(x.getGoodsCode()).getScItemId());
//            } else {
//                inventoryChecker.setGoodsId("-1");
//            }
//            inventoryChecker.setExcelId("BIVC" + x.getExcelId());
//            inventoryChecker.setCheckStatus(failStatus.getValue());
//            inventoryChecker.setCheckMsg(failStatus.getLabel());
//            inventoryChecker.setType(1);
//            create(inventoryChecker);
//        });
//        return Result.success(reslutList);
//
        return importBatchInventoryData2Check(request);
    }




    /**
     * 批次维度 的 校验总库存  6.27 新对账逻辑函数
     *
     * @param request
     * @return
     */
    public Result<List<InventoryCheckerRequest>> importBatchInventoryData2Check(ImportInventoryData2CheckRequest request) {

        //产品需求：只对分子公司的库存对账 其他组织员工对账(非103)给提示
        //避免高组织权限下平台数据很多 导致对账过程中大量平台冗余数据入库记录(导入100条 平台有10万条 那么每对账一次就会有9万9千条平台冗余数据入库)
        EmployeeGetCurrentLoginResponse employeeGetCurrentLoginResponse = bcOtsService.employeeGetCurrentLogin();
        if (!"103".equals(employeeGetCurrentLoginResponse.getRelatedOrganizationType())) {
            throw new FacadeException("OTS-03-001-01-15-113");
        }

        Set<String> goodsId = new HashSet<>();
        Set<String> warehouseCodes = new HashSet<>();
        List<InventoryCheckerRequest> reslutList = request.getInventoryData();
        String excelId = null;
        String excelName = null;
        log.info("批次库存导入校验");
        //遍历excel 手机货品编码/仓库编码 根据excel所有货品编码、仓库编码查询平台货品信息，仓库信息。并已货品分组，工厂编码+‘_’仓库编码分组
        if (CollectionUtils.isNotEmpty(reslutList)) {
            excelId = "BIVC" + reslutList.get(0).getExcelId();
            excelName = reslutList.get(0).getExcelName();
            reslutList.stream().forEach(x -> {
                        x.setGoodsCode(SapAndCrmFieldDealUtils.subStringScItmCode(x.getGoodsCode()));
                        if (StringUtils.isEmpty(x.getGoodsCode())
                                && StringUtils.isEmpty(x.getWarehouseCode())
                                && StringUtils.isEmpty(x.getFatoryCode())) {
                            //3个code都是空的 可能是最后一行 汇总行。 跳过
                            return;
                        }
                        if (StringUtils.isEmpty(x.getGoodsCode())
                                || StringUtils.isEmpty(x.getWarehouseCode())
                                || StringUtils.isEmpty(x.getFatoryCode())) {
                            x.setErrorMessage("excel数据有误，请检查工厂及仓库code 和物料编码不能为空");
                        } else {
                            goodsId.add(x.getGoodsCode());
                            warehouseCodes.add(x.getWarehouseCode());
                        }
                    }
            );
        }
        Map<String, ScItemSDO> goods = getGoodsListWithOuterId(goodsId);
        Map<String, WarehouseInfo> warehouses = getWarehouseList(warehouseCodes, true);

        //2022/6/27   拼装excel中对应的货品id，仓库id，并将excel数据录入临时表oui_batch_inventory_checker_temp
        try {
            if (CollectionUtils.isNotEmpty(reslutList)) {
                reslutList.stream().forEach(x -> {
                    if (StringUtils.isEmpty(x.getGoodsCode())
                            && StringUtils.isEmpty(x.getWarehouseCode())
                            && StringUtils.isEmpty(x.getFatoryCode())) {
                        //3个code都是空的 可能是最后一行 汇总行。 跳过
                        return;
                    }
                    //插入excel记录到临时表
                    BatchInventoryCheckerTemp batchInventoryCheckerTemp = new BatchInventoryCheckerTemp();
                    batchInventoryCheckerTemp.setExcelId("BIVC" +x.getExcelId());
                    batchInventoryCheckerTemp.setExcelName(x.getExcelName());
                    batchInventoryCheckerTemp.setBatchNo(x.getBatchNo());
                    batchInventoryCheckerTemp.setGoodsCode(x.getGoodsCode());
                    batchInventoryCheckerTemp.setFatoryCode(x.getFatoryCode());
                    batchInventoryCheckerTemp.setWarehouseCode(x.getWarehouseCode());

                    batchInventoryCheckerTemp.setUnrestrictedQuantity(QuantityFormatUtils.formatQuantityForDB(x.getTotalQuantity()).longValue());
                    batchInventoryCheckerTemp.setFrozenQuantity(QuantityFormatUtils.formatQuantityForDB(x.getFrozenQuantity()).longValue());
                    batchInventoryCheckerTemp.setInspectQuantity(QuantityFormatUtils.formatQuantityForDB(x.getInspectQuantity()).longValue());

                    //根据goodsCode拼装goodsId 方便后面join作为条件
                    if (goods.get(x.getGoodsCode()) != null) {
                        batchInventoryCheckerTemp.setGoodsId(goods.get(x.getGoodsCode()).getScItemId());
                    } else {
                        batchInventoryCheckerTemp.setCheckStatus(InventoryCheckStatus.GOODS_FAIL.getValue());
                        batchInventoryCheckerTemp.setCheckMsg(InventoryCheckStatus.GOODS_FAIL.getLabel());
                    }
                    //根据FatoryCode，WarehouseCode拼装WarehouseAreaId 方便后面join作为条件
                    if (warehouses.get(x.getFatoryCode() + "_" + x.getWarehouseCode()) != null) {
                        batchInventoryCheckerTemp.setWarehouseAreaId(warehouses.get(x.getFatoryCode() + "_" + x.getWarehouseCode()).getId());
                    } else {
                        batchInventoryCheckerTemp.setCheckStatus(InventoryCheckStatus.WAREHOUSE_FAIL.getValue());
                        batchInventoryCheckerTemp.setCheckMsg(InventoryCheckStatus.WAREHOUSE_FAIL.getLabel());
                    }

                    batchInventoryCheckerTempService.create1(batchInventoryCheckerTemp);
                }
                );
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("批次库存校验异常 插入excel数据"+e.getMessage());
            return Result.success(reslutList);
        }


        //获取当前用户权限下的逻辑仓库，作为比对平台库存中逻辑仓库的条件
        List<String> warehouseIdList = null;
        try {
            warehouseIdList = getGoodsListWithOuterId();
            log.info("批次库存校验查询当前登陆用户权限逻辑仓库封装结果 {} ",JSON.toJSONString(warehouseIdList));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("批次库存校验异常 查询当前用户权限下逻辑仓库"+e.getMessage());
        }

        try {
            //牧常 ----> 当前用户权限下没有仓库，统一按平台没有处理(SAP有 平台没有)
            if (warehouseIdList == null || CollectionUtils.isEmpty(warehouseIdList)) {
                String finalExcelName = excelName;
                String finalExcelId = excelId;
                reslutList.stream().forEach(x -> {
                    if (StringUtils.isEmpty(x.getGoodsCode())
                            && StringUtils.isEmpty(x.getWarehouseCode())
                            && StringUtils.isEmpty(x.getFatoryCode())) {
                        //3个code都是空的 可能是最后一行 汇总行。 跳过
                        return;
                    }
                    InventoryChecker inventoryChecker = new InventoryChecker();
                    inventoryChecker.setCheckStatus(InventoryCheckStatus.DEFECT_SAP_DATA.getValue());
                    inventoryChecker.setCheckMsg(InventoryCheckStatus.DEFECT_SAP_DATA.getLabel());
                    inventoryChecker.setFactoryCode(x.getFatoryCode());
                    inventoryChecker.setWarehouseCode(x.getWarehouseCode());
                    inventoryChecker.setGoodsCode(x.getGoodsCode());
                    inventoryChecker.setBatchNo(x.getBatchNo());
                    inventoryChecker.setExcelId(finalExcelId);
                    inventoryChecker.setExcelName(finalExcelName);
                    inventoryChecker.setType(1);
                    inventoryChecker.setWarehouseAreaId("-1");
                    inventoryChecker.setWarehouseId("-1");
                    inventoryChecker.setGoodsId("-1");

                    if (warehouses.get(x.getFatoryCode() + "_" + x.getWarehouseCode()) != null) {
                        inventoryChecker.setWarehouseAreaId(warehouses.get(x.getFatoryCode() + "_" + x.getWarehouseCode()).getId());
                        inventoryChecker.setWarehouseId(warehouses.get(x.getFatoryCode() + "_" + x.getWarehouseCode()).getWarehouseId());
                    }
                    if (goods.get(x.getGoodsCode()) != null) {
                        inventoryChecker.setGoodsId(goods.get(x.getGoodsCode()).getScItemId());
                    }
                    //SAP总库存 = 非限制 + 冻结 + 质检
                    long unrestrictedQuantiry = QuantityFormatUtils.formatQuantityForDB(x.getTotalQuantity()).longValue();
                    long frozenQuantity = QuantityFormatUtils.formatQuantityForDB(x.getFrozenQuantity()).longValue();
                    long inspectQuantity = QuantityFormatUtils.formatQuantityForDB(x.getInspectQuantity()).longValue();
                    long outTotalQuantity = unrestrictedQuantiry + frozenQuantity + inspectQuantity;
                    //SAP总库存
                    inventoryChecker.setOutTotalQuantity(outTotalQuantity);
                    //SAP冻结库存
                    inventoryChecker.setOutFrozenQuantity(frozenQuantity);
                    //SAP质检库存
                    inventoryChecker.setOutInspectQuantity(inspectQuantity);
                    inventoryChecker.setInTotalQuantity(0L);
                    inventoryChecker.setTotalQuantityGap(outTotalQuantity);
                    inventoryChecker.setInFrozenQuantity(0L);
                    inventoryChecker.setFrozenQuantityGap(frozenQuantity);
                    inventoryChecker.setInInspectQuantity(0L);
                    inventoryChecker.setInspectQuantityGap(inspectQuantity);
                    create(inventoryChecker);
                });
                return Result.success(reslutList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("批次库存校验异常 当前用户权限无仓库对账异常"+e.getMessage());
            return Result.success(reslutList);
        }

        try {

        // 2022/6/27 根据货品id，批次，仓库id 关联临时表 left join 库存表
        //左右表都有数据，比对总库存，冻结库存，质检库存。插入对账记录
        //temp有数据，库存表为空，表示sap批次库存未同步，提供：工厂、库存地点、物料编码、批次号。插入对账记录

            BatchInventoryCheckTempRequset batchInventoryCheckTempRequset = new BatchInventoryCheckTempRequset();
            batchInventoryCheckTempRequset.setExcelId(excelId);
            batchInventoryCheckTempRequset.setWarehouseIds(warehouseIdList);
            long l = System.currentTimeMillis();
            Result<Integer> LeftInventoryTotalResult = batchInventoryCheckerTempService.loadListLeftInventoryTotal(batchInventoryCheckTempRequset);
            long l1 = System.currentTimeMillis();
            log.info("批次库存对账左关联耗时 "+(l1 - l));
            int leftTotal = LeftInventoryTotalResult.getResult();
            int page = leftTotal % 1000 > 0 ? leftTotal/1000 + 1 : leftTotal/1000;
            batchInventoryCheckTempRequset.setLimit(1000);
            // 三种情况 1.SAP与平台匹配上 2.SAP有 平台缺失 3.导入是各种格式异常或者导入缺失异常 在上面基础校验已经处理 这里直接取就行
            for (int i = 0; i < page; i++) {
                batchInventoryCheckTempRequset.setStart(i*1000);
                log.info("批次库存对账loadListLeftInventory request {}",JSON.toJSONString(batchInventoryCheckTempRequset));
                Result<List<BatchInventoryCheckTempResponse>> batchInventoryCheckTempResponseResult = batchInventoryCheckerTempService.loadListLeftInventory(batchInventoryCheckTempRequset);
                if (CollectionUtils.isNotEmpty(batchInventoryCheckTempResponseResult.getResult())) {
                    for(BatchInventoryCheckTempResponse checkTempResponse : batchInventoryCheckTempResponseResult.getResult()) {
                        InventoryChecker inventoryChecker = new InventoryChecker();
                        inventoryChecker.setFactoryCode(checkTempResponse.getOutFatoryCode());
                        inventoryChecker.setWarehouseCode(checkTempResponse.getOutWarehouseCode());
                        inventoryChecker.setGoodsCode(checkTempResponse.getOutGoodsCode());
                        inventoryChecker.setBatchNo(checkTempResponse.getOutBatchNo());
                        inventoryChecker.setExcelId(excelId);
                        inventoryChecker.setExcelName(excelName);
                        inventoryChecker.setType(1);
                        inventoryChecker.setWarehouseAreaId("-1");
                        inventoryChecker.setWarehouseId("-1");
                        inventoryChecker.setGoodsId("-1");
                        inventoryChecker.setCheckStatus(InventoryCheckStatus.OK.getValue());
                        inventoryChecker.setCheckMsg(InventoryCheckStatus.OK.getLabel());
                        //判断基础校验是否失败
                        if (null != checkTempResponse.getCheckStatus()) {
                            inventoryChecker.setCheckStatus(checkTempResponse.getCheckStatus());
                            inventoryChecker.setCheckMsg(checkTempResponse.getCheckMsg());
                            inventoryChecker.setWarehouseAreaId(checkTempResponse.getTempWarehouseAreaId());
                            inventoryChecker.setGoodsId(checkTempResponse.getTempGoodsId());
                            if (warehouses.get(checkTempResponse.getOutFatoryCode() + "_" + checkTempResponse.getOutWarehouseCode()) != null) {
                                inventoryChecker.setWarehouseId(warehouses.get(checkTempResponse.getOutFatoryCode() + "_" + checkTempResponse.getOutWarehouseCode()).getWarehouseId());
                            }
                        } else {
                            //SAP有 平台没有 批次库存未同步
                            //SAP总库存 = 非限制 + 冻结 + 质检
                            long outTotalQuantity = checkTempResponse.getOutUnrestrictedQuantity() + checkTempResponse.getOutFrozenQuantity() + checkTempResponse.getOutInspectQuantity();
                            //SAP总库存
                            inventoryChecker.setOutTotalQuantity(outTotalQuantity);
                            //SAP冻结库存
                            inventoryChecker.setOutFrozenQuantity(checkTempResponse.getOutFrozenQuantity());
                            //SAP质检库存
                            inventoryChecker.setOutInspectQuantity(checkTempResponse.getOutInspectQuantity());
                            if (null == checkTempResponse.getInBatchNo()
                                    && null == checkTempResponse.getInGoodsId()
                                    && null == checkTempResponse.getInWarehouseAreaId()) {

                                inventoryChecker.setInTotalQuantity(0L);
                                inventoryChecker.setTotalQuantityGap(outTotalQuantity);

                                inventoryChecker.setInFrozenQuantity(0L);
                                inventoryChecker.setFrozenQuantityGap(checkTempResponse.getOutFrozenQuantity());

                                inventoryChecker.setInInspectQuantity(0L);
                                inventoryChecker.setInspectQuantityGap(checkTempResponse.getOutInspectQuantity());

                                inventoryChecker.setWarehouseAreaId(checkTempResponse.getTempWarehouseAreaId());
                                inventoryChecker.setGoodsId(checkTempResponse.getTempGoodsId());
                                if (warehouses.get(checkTempResponse.getOutFatoryCode() + "_" + checkTempResponse.getOutWarehouseCode()) != null) {
                                    inventoryChecker.setWarehouseId(warehouses.get(checkTempResponse.getOutFatoryCode() + "_" + checkTempResponse.getOutWarehouseCode()).getWarehouseId());
                                }

                                inventoryChecker.setCheckStatus(InventoryCheckStatus.DEFECT_SAP_DATA.getValue());
                                inventoryChecker.setCheckMsg(InventoryCheckStatus.DEFECT_SAP_DATA.getLabel());
                            } else {
                                //SAP与平台比对上库存
                                inventoryChecker.setInTotalQuantity(checkTempResponse.getInTotalQuantity());
                                inventoryChecker.setTotalQuantityGap(outTotalQuantity - checkTempResponse.getInTotalQuantity());

                                inventoryChecker.setInFrozenQuantity(checkTempResponse.getInFrozenQuantity());
                                inventoryChecker.setFrozenQuantityGap(checkTempResponse.getOutFrozenQuantity() - checkTempResponse.getInFrozenQuantity());

                                inventoryChecker.setInInspectQuantity(checkTempResponse.getInInspectQuantity());
                                inventoryChecker.setInspectQuantityGap(checkTempResponse.getOutInspectQuantity() - checkTempResponse.getInInspectQuantity());

                                inventoryChecker.setWarehouseAreaId(checkTempResponse.getTempWarehouseAreaId());
                                inventoryChecker.setGoodsId(checkTempResponse.getTempGoodsId());
                                if (warehouses.get(checkTempResponse.getOutFatoryCode() + "_" + checkTempResponse.getOutWarehouseCode()) != null) {
                                    inventoryChecker.setWarehouseId(warehouses.get(checkTempResponse.getOutFatoryCode() + "_" + checkTempResponse.getOutWarehouseCode()).getWarehouseId());
                                }

                                if (inventoryChecker.getTotalQuantityGap() != 0
                                    || inventoryChecker.getFrozenQuantityGap() != 0
                                    || inventoryChecker.getInspectQuantityGap() != 0) {
                                    inventoryChecker.setCheckStatus(InventoryCheckStatus.INV_QUANTITY_FAIL.getValue());
                                    inventoryChecker.setCheckMsg(InventoryCheckStatus.INV_QUANTITY_FAIL.getLabel());
                                }

                            }
                        }
                        create(inventoryChecker);
                    }
                }
            }




            // 2022/6/27 根据货品id，批次，仓库id 库存表 left join 临时表
            //关联带条件：临时表匹配记录为null,表示平台有冗余数据，将冗余的SAP数据，默认为0值进行匹配。插入对账记录
            BatchInventoryCheckTempRequset batchInventoryCheckTempRequsetRight = new BatchInventoryCheckTempRequset();
            batchInventoryCheckTempRequsetRight.setExcelId(excelId);
            batchInventoryCheckTempRequsetRight.setWarehouseIds(warehouseIdList);
            long l2 = System.currentTimeMillis();
            Result<Integer> RightInventoryTotalResult = batchInventoryCheckerTempService.loadListRightInventoryTotal(batchInventoryCheckTempRequsetRight);
            long l3 = System.currentTimeMillis();
            log.info("批次库存对账右关联耗时 "+(l3 - l2));
            int rightTotal = RightInventoryTotalResult.getResult();
            int rightPage = rightTotal % 1000 > 0 ? rightTotal/1000 + 1 : rightTotal/1000;
            batchInventoryCheckTempRequsetRight.setLimit(1000);
            //平台冗余数据 比SAP多
            for (int i = 0; i < rightPage; i++) {
            batchInventoryCheckTempRequsetRight.setStart(i * 1000);
            log.info("批次库存对账loadListRightInventory request {}",JSON.toJSONString(batchInventoryCheckTempRequsetRight));
            Result<List<BatchInventoryCheckTempResponse>> batchInventoryCheckTempResponseResult = batchInventoryCheckerTempService.loadListRightInventory(batchInventoryCheckTempRequsetRight);
            if (CollectionUtils.isNotEmpty(batchInventoryCheckTempResponseResult.getResult())) {
                for(BatchInventoryCheckTempResponse checkTempResponse : batchInventoryCheckTempResponseResult.getResult()) {
                    //只处理sap没有 平台库存表
                    if (checkTempResponse.getOutBatchNo() == null
                        && checkTempResponse.getTempGoodsId() == null
                        && checkTempResponse.getTempWarehouseAreaId() == null) {
                            InventoryChecker inventoryChecker = new InventoryChecker();
                            inventoryChecker.setGoodsId(checkTempResponse.getInGoodsId());
                            inventoryChecker.setBatchNo(checkTempResponse.getInBatchNo());
                            inventoryChecker.setWarehouseAreaId(checkTempResponse.getInWarehouseAreaId());
                            inventoryChecker.setWarehouseId("-1");
                            inventoryChecker.setExcelId(excelId);
                            inventoryChecker.setExcelName(excelName);
                            inventoryChecker.setType(1);

                            //总库存校验 将sap数据默认设置为0处理
                            inventoryChecker.setOutTotalQuantity(0L);
                            inventoryChecker.setInTotalQuantity(checkTempResponse.getInTotalQuantity());
                            inventoryChecker.setTotalQuantityGap(0 - checkTempResponse.getInTotalQuantity());

                            //冻结库存校验 将sap数据默认设置为0处理
                            inventoryChecker.setOutFrozenQuantity(0L);
                            inventoryChecker.setInFrozenQuantity(checkTempResponse.getInFrozenQuantity());
                            inventoryChecker.setFrozenQuantityGap(0 - checkTempResponse.getInFrozenQuantity());

                            //质检库存校验将sap数据默认设置为0处理
                            inventoryChecker.setOutInspectQuantity(0L);
                            inventoryChecker.setInInspectQuantity(checkTempResponse.getInInspectQuantity());
                            inventoryChecker.setInspectQuantityGap(0 - checkTempResponse.getInInspectQuantity());
                            inventoryChecker.setCheckStatus(InventoryCheckStatus.OTS_DATA_SURPLUS.getValue());
                            inventoryChecker.setCheckMsg(InventoryCheckStatus.OTS_DATA_SURPLUS.getLabel());

                            create(inventoryChecker);
                    }
                }

            }

        }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("批次库存校验异常 对比平台库存数据"+e.getMessage());
        }

        return Result.success(reslutList);
    }





    /**
     * 获取当前用户逻辑仓库
     *
     * @return
     */
    private List<String> getGoodsListWithOuterId() {
        List<String> warehouseAreaIds = new ArrayList<>();
        LoadWarehouseResponse loadWarehouseResponse = batchInventoryCheckerTempService.loadWarehouse();
        log.info("批次库存校验查询当前登陆用户权限逻辑仓库 {} ",JSON.toJSONString(loadWarehouseResponse));
        if (loadWarehouseResponse != null && CollectionUtils.isNotEmpty(loadWarehouseResponse.getResult())) {
            for (Object warehouse : loadWarehouseResponse.getResult()) {
                Map map = JSONObject.parseObject(JSONObject.toJSONString(warehouse),Map.class);
                if (map.get("id") != null) {
                    warehouseAreaIds.add(map.get("id").toString());
                }
            }
        }
        return warehouseAreaIds;
    }






    /**
     * 获取货品 通过外部id
     *
     * @param goodsCode
     * @return
     */
    private Map<String, ScItemSDO> getGoodsListWithOuterId(Set<String> goodsCode) {
        SearchScItemRequest searchScItemRequest = new SearchScItemRequest();
        Map<String, List<String>> outerIdList = new HashMap<>();
        outerIdList.put("outerIdList", goodsCode.stream().collect(Collectors.toList()));
        outerIdList.put("statusList", Arrays.asList("1", "-1"));
        searchScItemRequest.setPageSize(goodsCode.size());
        searchScItemRequest.setSearchReqDTO(outerIdList);
        Result<List<ScItemSDO>> scItemResult = baseDataService.searchScItem(searchScItemRequest);
        if (scItemResult == null) {
            throw new FacadeException("OTS-03-001-01-15-002", JSON.toJSONString(goodsCode));
        }
        if (org.apache.commons.collections4.CollectionUtils.isEmpty(scItemResult.getResult())) {
            throw new FacadeException("OTS-03-001-01-15-015", JSON.toJSONString(goodsCode));
        }
        Map<String, ScItemSDO> scItems = scItemResult.getResult().stream().collect(Collectors.toMap(x -> x.getOuterId(), x -> x, (v1, v2) -> v2));
        return scItems;
    }

    /**
     * 获取货品 通过内部id
     *
     * @param goodsCode
     * @return
     */
    private Map<String, ScItemSDO> getGoodsList(Set<String> goodsCode) {
        SearchScItemRequest searchScItemRequest = new SearchScItemRequest();
        Map<String, List<String>> outerIdList = new HashMap<>();
        outerIdList.put("scItemIds", goodsCode.stream().collect(Collectors.toList()));
        outerIdList.put("statusList", Arrays.asList("1", "-1"));
        searchScItemRequest.setPageSize(goodsCode.size());
        searchScItemRequest.setSearchReqDTO(outerIdList);
        Result<List<ScItemSDO>> scItemResult = baseDataService.searchScItem(searchScItemRequest);
        if (scItemResult == null) {
            return new HashMap<>();
        }
        if (org.apache.commons.collections4.CollectionUtils.isEmpty(scItemResult.getResult())) {
            return new HashMap<>();
        }
        Map<String, ScItemSDO> scItems = scItemResult.getResult().stream().collect(Collectors.toMap(x -> x.getScItemId(), x -> x, (v1, v2) -> v2));
        return scItems;
    }

    /**
     * 获取仓库
     */
    private Map<String, WarehouseInfo> getWarehouseList(Set<String> warehouseCodes, boolean useCodeAskey) {
        Map<String, WarehouseInfo> resultMap = new HashMap<>();
        List<String> warehouseCodeList = new ArrayList(warehouseCodes);
        List<List<String>> codesPartition = Lists.partition(warehouseCodeList, 10);
        for (List<String> tmp : codesPartition) {
            SearchCargoZoneListRequest request = new SearchCargoZoneListRequest();
            if (useCodeAskey) {
                request.setWarehouseCodes(new ArrayList(tmp));
            } else {
                request.setWarehouseAreaIds(new ArrayList(tmp));
            }
            //通一逻辑仓code 可能对应多个工厂code，为了保险，10个逻辑仓code查 就匹配100条数据足够了
            request.setLimit(100);
            SearchCargoZoneListResponse resp = unifiedInventoryService.searchCargoZoneList(request);
            if (resp.getResult() != null) {
                if (useCodeAskey) {
                    resp.getResult().stream().forEach(x -> {
                        resultMap.put(x.getSapFactoryCode() + "_" + x.getWarehouseCode(), x);
                    });
                } else {
                    resp.getResult().stream().forEach(x -> {
                        resultMap.put(x.getId(), x);
                    });
                }
            }
        }
        return resultMap;
    }

    /**
     * 获取库存
     *
     * @param goodsId
     * @param warehouseAreaId
     * @param batchNo
     * @param type            2批次  1仓库库存
     * @return
     */
    private InventoryQueryResultForCheck getBizInventory(String goodsId, String warehouseAreaId, String batchNo, int type) {
        GetListBatchStockForCheckRequest inventoryRequest = new GetListBatchStockForCheckRequest();
        inventoryRequest.setGoodsId(goodsId);
        inventoryRequest.setWarehouseAreaId(warehouseAreaId);
        inventoryRequest.setBatchNo(batchNo);
        inventoryRequest.setType(type);
        GetListBatchStockForCheckResponse resp = otsUnifiedInventoryService.getListBatchStockForCheck(inventoryRequest);
        if (CollectionUtils.isNotEmpty(resp.getResult())) {
            return resp.getResult().get(0);
        }
        return null;
    }

    @Override
    public Result<List<InventoryCheckDetailResult>> getCheckResult(GetCheckResultRequest getCheckResultRequest) {
        GetTargetEexcelResultRequest request = new GetTargetEexcelResultRequest();
        GetTargetEexcelTotalRequest totaoRequest = new GetTargetEexcelTotalRequest();
        BeanUtils.copyProperties(getCheckResultRequest, request);
        BeanUtils.copyProperties(getCheckResultRequest, totaoRequest);
        GetTargetEexcelResultResponse response = getTargetEexcelResult(request);
        Result<List<PageTotalResponse>> pageResponse = getTargetEexcelTotal(totaoRequest);
        if (CollectionUtils.isEmpty(response.getResult())) {
            return Result.listEmptySuccess();
        }
        List<InventoryCheckDetailResult> resp = new ArrayList<>();
        Set<String> goodsIds = new HashSet<>();
        Set<String> warehouseAreaIds = new HashSet<>();
        Set<String> warehouseIds = new HashSet<>();
        response.getResult().stream().forEach(x -> {
            InventoryCheckDetailResult result = new InventoryCheckDetailResult();
            if (StringUtils.isNotBlank(x.getGoodsId()) && !"-1".equals(x.getGoodsId())) {
                goodsIds.add(x.getGoodsId());
            }
            if (StringUtils.isNotBlank(x.getWarehouseAreaId()) && !"-1".equals(x.getWarehouseAreaId())) {
                warehouseAreaIds.add(x.getWarehouseAreaId());
                warehouseIds.add(x.getWarehouseId());
            }
            BeanUtils.copyProperties(x, result);
            result.setInAvailabelQuantityFormat(formartQuantity(x.getInAvailabelQuantity()));
            result.setInTotalQuantityFormat(formartQuantity(x.getInTotalQuantity()));
            result.setOutAvailabelQuantityFormat(formartQuantity(x.getOutAvailabelQuantity()));
            result.setOutTotalQuantityFormat(formartQuantity(x.getOutTotalQuantity()));
            result.setAvailabelQuantityGapFormat(formartQuantity(x.getAvailabelQuantityGap()));
            result.setTotalQuantityGapFormat(formartQuantity(x.getTotalQuantityGap()));
            result.setErrorMessage(x.getCheckMsg());
            result.setOutWithholdingQuantityFormat(formartQuantity(x.getOutWithholdingQuantity()));
            result.setInWithholdingQuantityFormat(formartQuantity(x.getInWithholdingQuantity()));
            result.setOutInspectQuantityFormat(formartQuantity(x.getOutInspectQuantity()));
            result.setInInspectQuantityFormat(formartQuantity(x.getInInspectQuantity()));
            result.setInspectQuantityGapFormat(formartQuantity(x.getInspectQuantityGap()));
            result.setOutFrozenQuantityFormat(formartQuantity(x.getOutFrozenQuantity()));
            result.setInFrozenQuantityFormat(formartQuantity(x.getInFrozenQuantity()));
            result.setFrozenQuantityGapFormat(formartQuantity(x.getFrozenQuantityGap()));

            resp.add(result);
        });
        Map<String, ScItemSDO> goods = getGoodsList(goodsIds);
        Map<String, WarehouseInfo> warehouseAreas = getWarehouseList(warehouseAreaIds, false);
        Map<String, WarehouseInfo> warehouses = getWarehouseList(warehouseIds, false);
        resp.stream().forEach(x -> {
            x.setGoodsCode(x.getGoodsId());
            x.setGoodsName(x.getGoodsId());
            ScItemSDO goodsInfo = goods.get(x.getGoodsId());
            if (goodsInfo != null) {
                x.setGoodsCode(goodsInfo.getOuterId());
                x.setGoodsName(goodsInfo.getTitle());
            }
            x.setWarehouseName("-");
            x.setWarehouseAreaName("-");
            if (warehouses.get(x.getWarehouseId()) != null) {
                x.setWarehouseName(warehouses.get(x.getWarehouseId()).getName());
            }
            if (warehouseAreas.get(x.getWarehouseAreaId()) != null) {
                x.setWarehouseAreaName(warehouseAreas.get(x.getWarehouseAreaId()).getName());
            }
        });
        return Result.listSuccess(resp, pageResponse.getResult().get(0).getTotal());
    }

    private String formartQuantity(Long quantity) {
        if (quantity == null) {
            return "-";
        }
        return QuantityFormatUtils.getQuantityForPage(quantity.toString()).toString();
    }

    /**
     * 计划调拨单对账
     *
     * @param importAndCompareRequest
     * @return
     */
    public ImportAndCompareResponse importAndCompare(ImportAndCompareRequest importAndCompareRequest) {
        List<String> errorList = new ArrayList<>();
        int successCount = 0;

        if (CollectionUtils.isEmpty(importAndCompareRequest.getImportData())) {
            return new ImportAndCompareResponse();
        }

        for (ImportInventoryCompareRequest request : importAndCompareRequest.getImportData()) {
            AllocateOrderDetail detail = getAllocateOrderDetailByOutAllocateIdAndItemId(request.getOrderNo(), request.getScItemOutId());
            if (Objects.isNull(detail)) {
                errorList.add(request.getOrderNo());
                continue;
            }
            //对比项：销售订单数量\确认数量\到货数量\交货单数量\出库数量
            if (detail.getQuantity().equalsIgnoreCase(request.getSaleOrderQuantity()) &&
                    detail.getConfirmQuantity().equalsIgnoreCase(request.getComfirmQuantity()) &&
                    detail.getDeliveryQuantity().equals(request.getDeliveryQuantity()) &&
                    detail.getReceiveQuantity().toString().equals(request.getReceivedQuantity()) &&
                    detail.getShipQuantity().toString().equals(request.getOutQuantity())) {
                successCount++;
                continue;
            }
            errorList.add(request.getOrderNo());
        }

        ImportAndCompareResponse response = new ImportAndCompareResponse();
        response.setResult(errorList);
        response.setSuccessCount(successCount);
        response.setSuccess(errorList.isEmpty()?Boolean.TRUE: Boolean.FALSE);
        response.setFailureCount(errorList.size());
        log.info("importAndCompare result:{}", JSONObject.toJSONString(response));
        return response;
    }

    /**
     * 根据计划调拨单外部单号和货品外部单号获取计划调拨单明细
     *
     * @param outAllocateId
     * @param outItemId
     * @return
     */
    private AllocateOrderDetail getAllocateOrderDetailByOutAllocateIdAndItemId(String outAllocateId, String outItemId) {
        AllocateOrderDetailLoadListRequest allocateOrderDetailRequest = new AllocateOrderDetailLoadListRequest();
        AllocateOrderLoadListRequest allocateOrderLoadListRequest = new AllocateOrderLoadListRequest();
        allocateOrderLoadListRequest.setOutAllocateOrderId(outAllocateId);
        Result<List<AllocateOrder>> orderResult = allocateOrderService.loadAllocateOrderList(allocateOrderLoadListRequest);
        if (CollectionUtils.isEmpty(orderResult.getResult())) {
            return null;
        }
        allocateOrderDetailRequest.setAllocateOrderId(String.valueOf(orderResult.getResult().get(0).getId()));
        Result<List<AllocateOrderDetail>> result = allocateOrderDetailService.loadAllocateOrderDetailList(allocateOrderDetailRequest);
        if (CollectionUtils.isEmpty(result.getResult())) {
            return null;
        }
        for (AllocateOrderDetail detail : result.getResult()) {
            if (outItemId.equals(detail.getScItemCode())) {
                return detail;
            }
        }

        return null;
    }

    /**
     * 批次维度 的 校验可用库存
     *
     * @param request
     * @return
     */
    @Override
    public Result<InventoryCheckResult> importGoodsInventoryData2Check(ImportGoodsInventoryData2CheckRequest request) {
        Set<String> goodsId = new HashSet<>();
        Set<String> warehouseCodes = new HashSet<>();
        //bivc:business inventory check
        if (CollectionUtils.isNotEmpty(request.getInventoryData())) {
            request.getInventoryData().stream().forEach(x -> {
                        x.setGoodsCode(SapAndCrmFieldDealUtils.subStringScItmCode(x.getGoodsCode()));
                        if (StringUtils.isEmpty(x.getGoodsCode())
                                || StringUtils.isEmpty(x.getWarehouseCode())
                                || StringUtils.isEmpty(x.getFatoryCode())) {
                            x.setErrorMessage("excel数据有误，请检查工厂及仓库code 和物料编码不能为空");
                        } else {
                            goodsId.add(x.getGoodsCode());
                            warehouseCodes.add(x.getWarehouseCode());
                        }
                    }
            );
        }
        Map<String, ScItemSDO> goods = getGoodsListWithOuterId(goodsId);
        Map<String, WarehouseInfo> warehouses = getWarehouseList(warehouseCodes, true);
        List<InventoryCheckerRequest> inventoryCheckerList = new ArrayList<>();
        request.getInventoryData().stream().forEach(x -> {
            InventoryChecker inventoryChecker = new InventoryChecker();
            WarehouseInfo warehouseInfo = warehouses.get(x.getFatoryCode() + "_" + x.getWarehouseCode());
            inventoryChecker.setWarehouseCode(x.getWarehouseCode());
            inventoryChecker.setFactoryCode(x.getFatoryCode());
            inventoryChecker.setGoodsCode(x.getGoodsCode());
            inventoryChecker.setExcelName(x.getExcelName());
            InventoryCheckStatus failStatus = InventoryCheckStatus.OK;
            //货品code 仓库code不能为空
            if (warehouseInfo == null) {
                failStatus = InventoryCheckStatus.WAREHOUSE_FAIL;
            }
            if (goods.get(x.getGoodsCode()) == null) {
                failStatus = InventoryCheckStatus.GOODS_FAIL;
            }
            BigDecimal availabelQuantity = new BigDecimal(0);
            BigDecimal saleWithholdingQuantity = new BigDecimal(0);
            try {
                availabelQuantity = QuantityFormatUtils.formatQuantityForDB(x.getAvailabelQuantity());
                saleWithholdingQuantity = QuantityFormatUtils.formatQuantityForDB(x.getSaleWithHoldQuantity());
            } catch (Exception e) {
                failStatus = InventoryCheckStatus.QUANTITY_FORMAT_FAIL;
            }
            long availabelGap = 0;
            InventoryQueryResultForCheck inventoryCheck = null;
            if (failStatus.getValue() > 0) {
                inventoryCheck = getBizInventory(goods.get(x.getGoodsCode()).getScItemId(), warehouseInfo.getId(), null, 1);
                if (inventoryCheck == null) {
                    failStatus = InventoryCheckStatus.INVENTORY_FAIL;
                }
                if (inventoryCheck != null) {
                    //共享仓 没有销售预占 有调拨单预占， 进行库存比较时 拿我们的额总库存与SAP可用进行比较 不用可用库存直接比
                    //
                    //0317调整为不关什么仓库 都是sap的（可用+预占)-(平台的可用+预占)
                    availabelGap = availabelQuantity.longValue() +
                            saleWithholdingQuantity.longValue() -
                            inventoryCheck.getAvailableQuantity() - inventoryCheck.getWithholdingQuantity();
                    //0317前
//                    if (warehouseInfo != null && warehouseInfo.getType() == 2) {
//                        availabelGap = availabelQuantity.longValue()+
//                                saleWithholdingQuantity.longValue() -
//                                inventoryCheck.getTotalQuantity()-inventoryCheck.getWithholdingQuantity();
//                    } else {
//                        availabelGap = availabelQuantity.longValue()-inventoryCheck.getAvailableQuantity();
//                    }
                }

                if (availabelGap != 0) {
                    failStatus = InventoryCheckStatus.INV_QUANTITY_FAIL;
                }
                if (failStatus.getValue() < 0) {
                    inventoryCheckerList.add(x);
                }
            }
            inventoryChecker.setBatchNo(x.getBatchNo());
            inventoryChecker.setOutAvailabelQuantity(QuantityFormatUtils.formatQuantityForDB(x.getAvailabelQuantity()).longValue());
            inventoryChecker.setAvailabelQuantityGap(availabelGap);
            inventoryChecker.setOutWithholdingQuantity(QuantityFormatUtils.formatQuantityForDB(x.getSaleWithHoldQuantity()).longValue());

            if (inventoryCheck != null) {
                inventoryChecker.setInventoryId(inventoryCheck.getId());
                inventoryChecker.setInAvailabelQuantity(inventoryCheck.getAvailableQuantity());
                inventoryChecker.setInWithholdingQuantity(inventoryCheck.getWithholdingQuantity());
            }
            if (warehouseInfo != null) {
                inventoryChecker.setWarehouseAreaId(warehouseInfo.getId());
                inventoryChecker.setWarehouseId(warehouseInfo.getWarehouseId());
            } else {
                inventoryChecker.setWarehouseAreaId("-1");
                inventoryChecker.setWarehouseId("-1");
            }
            if (goods.get(x.getGoodsCode()) != null) {
                inventoryChecker.setGoodsId(goods.get(x.getGoodsCode()).getScItemId());
            } else {
                inventoryChecker.setGoodsId("-1");
            }
            inventoryChecker.setExcelId("BIVC" + x.getExcelId());
            inventoryChecker.setCheckStatus(failStatus.getValue());
            inventoryChecker.setCheckMsg(failStatus.getLabel());
            inventoryChecker.setType(2);
            create(inventoryChecker);
        });
        InventoryCheckResult inventoryCheckResult = new InventoryCheckResult();
        return Result.success(inventoryCheckResult);
    }
}
